﻿/*********************************************************
 * 开发人员：Tony
 * 创建时间：2013-4-8 14:56:38
 * 描述说明：
 * 
 * 更改历史：
 * 
 * *******************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Beway.ValueAdded.Model;
using Beway.ValueAdded.Model.ViewModel;

namespace Beway.ValueAdded.Service
{
    public class ReUserServ
    {
        /// <summary>
        /// 通过用户ID，查找实体
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public Model.ReUser GetByUserId(int userId)
        {
            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {
                var u = db.ReUser.SingleOrDefault(m => m.UserID == userId);
                return u;
            }
        }

        /// <summary>
        /// 获得所有商户信息
        /// </summary>
        /// <returns></returns>
        public List<ReUser> SelectAll()
        {
            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {
               return db.ReUser.ToList();
            }
        }

        /// <summary>
        /// 新增充值账户
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public int AddReUser(ReUser Model)
        {
            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {
                db.ReUser.Add(Model);
                db.SaveChanges();
                return Model.UserID;
            }
        }


        public int UpdateReUser(ReUser Model)
        {
            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {
                ReUser q = db.ReUser.FirstOrDefault(m => m.UserID == Model.UserID);
                if (Model != null)
                {
                    q.UserName = Model.UserName;
                    q.UserTitle = Model.UserTitle;
                    q.VerifyMac = Model.VerifyMac;
                    q.Contact = Model.Contact;
                    q.Mobile = Model.Mobile;
                    q.RechStatus = Model.RechStatus;
                    q.AdminId = Model.AdminId;
                    q.BeginTime = Model.BeginTime;
                    q.EndTime = Model.EndTime;
                    //q.RechAmount = Model.RechAmount;
                    //q.RechStock = Model.RechStock;
                    q.RechDayTotal = Model.RechDayTotal;
                    q.RechAlarm = Model.RechAlarm;
                    q.SuppId = Model.SuppId;
                    q.RechIpWhiteList = Model.RechIpWhiteList;
                    q.RechDiscountRate = Model.RechDiscountRate;
 
                    return db.SaveChanges();
                }
                else
                {
                    return 0;
                }
            }
        }

        /// <summary>
        /// 更新充值额度的余额(RechStock)
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        public int UpdateStock(ReUser user)
        {
            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {
                ReUser q = db.ReUser.FirstOrDefault(m => m.UserID == user.UserID);
                if (user != null)
                {                      
                    q.RechStock = user.RechStock;
                    return db.SaveChanges();
                }
                else
                {
                    return 0;
                }
            }
        }

        /// <summary>
        /// 删除充值账户
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int DelReUser(int id)
        {
            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {
                if (db.Recharge.Count(m => m.UserID == id) > 0)//此账户下有充值记录不能删除
                {
                    return 0;
                }
                else
                {
                    ReUser Model = db.ReUser.FirstOrDefault(m => m.UserID == id);
                    db.ReUser.Remove(Model);
                    return db.SaveChanges();
                }                
            }
        }

        public List<ReUserView> ReUserList(int page, int rows, string UserName, int adminId, out int ListCount)
        {
            using (BewayValueAddedEntities db = new BewayValueAddedEntities())
            {
                IQueryable<ReUserView> q = from r in db.ReUser
                                           join a in db.AdminUser on r.AdminId equals a.AdminID into admin
                                           from ad in admin.DefaultIfEmpty()
                                           select new ReUserView
                                           {
                                               AddTime = r.AddTime,
                                               AdminId = r.AdminId,
                                               AdminMobile = ad.Mobile,
                                               AdminTrueName = ad.TrueName,
                                               BeginTime = r.BeginTime,
                                               EndTime = r.EndTime,
                                               Mobile = r.Mobile,
                                               Password = r.Password,
                                               RechAlarm = r.RechAlarm,
                                               RechAmount = r.RechAmount,
                                               RechStatus = r.RechStatus,
                                               RechStock = r.RechStock,
                                               SuppId = r.SuppId,                                               
                                               UserID = r.UserID,
                                               UserName = r.UserName,
                                               UserTitle = r.UserTitle,
                                               VerifyMac = r.VerifyMac,
                                               RechDiscountRate = r.RechDiscountRate,
                                               RechIpWhiteList = r.RechIpWhiteList,
                                               RechDayTotal = r.RechDayTotal
                                           };


                if (UserName != null && UserName != "")
                {
                    q = q.Where(m => m.UserTitle.Contains(UserName));
                }
                if (adminId > 0)
                {
                    q = q.Where(m => m.AdminId == adminId);
                }
                q = q.OrderByDescending(m => m.UserID);

                ListCount = q.Count();

                var adm = q.Skip((page - 1) * rows).Take(rows).ToList();
                return adm;
            }
        }
    }
}
